import sys
import os
import re
import ConfigParser
import globalvar
from optparse import OptionParser
import struct
import stat
import binascii
import base64
import datetime

Version = 0x0        
RawImagePkg = 0x1
RawImagePkgBP = 0x2
FSUpdatePkg = 0x4

def unsigned(n):
    return n & 0xFFFFFFFF

        
def upc_gen(infile,vendor,model,upcname,oldversion,newversion,desc,outfile):
    #write the version
    outf=open(outfile,'wb')
    outf.write("<?xml version=\"1.0\"?>\n")
    outf.write("<update-package-catalog>\n")
    
    outf.write("<manufacturer>%s</manufacturer>\n"%(vendor))
    outf.write("<model>%s</model>\n"%(model))
    
    outf.write("<update-package>\n")
    outf.write("<format>4</format>\n")
    outf.write("<name>%s</name>\n"%(upcname))
    outf.write("<original-version>%s</original-version>\n"%(oldversion))
    outf.write("<updated-version>%s</updated-version>\n"%(newversion))
    dn = datetime.datetime.now()
    dn_str = dn.strftime('%Y/%m/%d %H:%M:%S')    
    outf.write("<creation-date>%s</creation-date>\n"%(dn_str))
    
    outf.write("<description>%s</description>\n"%(desc))
    size = os.stat(infile)[stat.ST_SIZE];
    outf.write("<size>%d</size>\n"%(size))
    
    inf = open(infile,'rb')
    alldata = inf.read(size)  
    crcsize=binascii.crc32(alldata)  
    crcsize= unsigned(crcsize)
    inf.close()
    outf.write("<crc>%u</crc>\n"%(crcsize))
    alldata_base64 = base64.b64encode(alldata)
    outf.write("<binary>%s</binary>\n"%(alldata_base64))

    outf.write("</update-package>\n")

        
    outf.write("</update-package-catalog>\n")

    outf.close()
    if globalvar.dmlogging:
        globalvar.dmlogging.debug('upc gen (infile = %s,vendor=%s,model=%s,upcname=%s,oldversion=%s,newversion=%s,desc=%s,outfile=%s)'%(infile,vendor,model,upcname,oldversion,newversion,desc,outfile))    
    return 0
    

def main(*argv):
    """Generate the diff package using the """
    if argv:
        argv = list(argv)
    else:
        argv = sys.argv[1:]
        
    usage = "usage: fs_diff -i [infile]"    
    parser = OptionParser(usage=usage)    
    
    parser.add_option("-i", "--infile", dest="infile",type="string",help="input upk file  ")
    parser.add_option("-v", "--vendor", dest="vendor",type="string",help="the vendor ")
    parser.add_option("-m", "--model", dest="model",type="string",help="the model ")
    parser.add_option("-u", "--upcname", dest="upcname",type="string",help="upcname  ")
    parser.add_option("-o", "--oldversion", dest="oldversion",type="string",help="oldversion  ")
    parser.add_option("-n", "--newversion", dest="newversion",type="string",help="newversion  ")
    parser.add_option("-d", "--desc", dest="desc",type="string",help="desc")
    parser.add_option("-t", "--outfile", dest="outfile",type="string",help="outfile target file")

    (options, args) = parser.parse_args(args=argv)
    
    if options.infile:
        infile = os.path.abspath(options.infile)
    
    ret = upc_gen(options.infile,options.vendor,options.model,options.upcname,
                  options.oldversion,options.newversion,options.desc,options.outfile)
       
    print ret
        
if __name__ == "__main__":
    main()
    